package com.hydra.baseui.recyclerlist;

import android.app.Activity;
import androidx.annotation.IdRes;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

/**
 * Created by Hydra on 2017/1/21.
 *
 */
public class UIHelper {

    public static <T extends View> T id(View rootView, @IdRes int resId) {
        return (T) rootView.findViewById(resId);
    }

    public static <T extends View> T id(Activity activity, @IdRes int resId) {
        return (T) activity.findViewById(resId);
    }

    public static void measureView(View child) {
        LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
        }

        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;

        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }
}
